package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.annotaion.RepeatSubmit;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.views.*;
import com.xmy.cultivate.excel.ExcelEnrollRecord;
import com.xmy.cultivate.mapper.EnrollInfoMapper;
import com.xmy.cultivate.service.*;
import com.xmy.cultivate.util.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 招生数据 前端控制器
 * </p>
 *
 * @author min
 * @since 2023-02-10
 */
@RestController
@RequestMapping("/api/enrollInfo")
@Scope("prototype")
public class EnrollInfoController extends BaseController {

    @Autowired
    @Lazy
    IEnrollInfoService iEnrollInfoService;

    @Autowired
    @Lazy
    IOrderDetailService iOrderDetailService;

    @Autowired
    @Lazy
    IStaffService iStaffService;

    @Autowired
    @Lazy
    IStudentService iStudentService;

    @Autowired
    @Lazy
    EnrollInfoMapper enrollInfoMapper;

    @Autowired
    @Lazy
    IOrderApplyService iOrderApplyService;

    @Autowired
    IDealInfoService iDealInfoService;

    @Autowired
    @Lazy
    IEnrollSetService iEnrollSetService;

    @Autowired
    @Lazy
    IEnrollCustomerService iEnrollCustomerService;

    @Autowired
    @Lazy
    IdWorker idWorker;

    @RequiresPermissions(value = "manage:enroll_info")
    @GetMapping("/getEnrollInfoList/{orderId}")
    public Result getEnrollInfoList(@PathVariable(value = "orderId") String orderId) {
        List<EnrollInfo> enrollInfoList = iEnrollInfoService.getEnrollInfoList(orderId);
        return new Result(Code.OK, new PageResult(enrollInfoList, (long) enrollInfoList.size(), 1L));
    }

    @DeleteMapping("/delEnrollInfo/{id}")
    public Result delEnrollInfo(@PathVariable(value = "id") String id) {
        EnrollInfo enrollInfo = enrollInfoMapper.selectById(id);
        EnrollInfo enrollInfoGrant = iEnrollInfoService.getGrantCount(enrollInfo.getOrderId());
        if (enrollInfoGrant != null) {
            if (enrollInfoGrant.getIsGrant() > 0) {
                return new Result(Code.ERROR, "", "订单已有" + enrollInfoGrant.getTeacherName() + "老师发放提成，不能删除");
            }
        }

        iEnrollInfoService.delEnrollInfo(id);
        return new Result(Code.OK, "", "");
    }

    /**
     * 从订单弹窗添加战队成员
     *
     * @param orderApply
     * @return
     */
    @PostMapping("/addEnrollInfo")
    public Result addEnrollInfo(@RequestBody OrderApply orderApply) {
        QueryWrapper<DealInfo> dealInfoQueryWrapper = new QueryWrapper<>();
        dealInfoQueryWrapper.eq("order_id", orderApply.getId());
        Long count = iDealInfoService.count(dealInfoQueryWrapper);
        if (count > 0) {
            return new Result(Code.ERROR, "", "已存在续报信息，不能添加，如要添加，请先删除续报信息");
        }

        EnrollInfo enrollInfoGrant = iEnrollInfoService.getGrantCount(orderApply.getId());
        if (enrollInfoGrant != null) {
            if (enrollInfoGrant.getIsGrant() > 0) {
                return new Result(Code.ERROR, "", "订单已有" + enrollInfoGrant.getTeacherName() + "老师发放提成，不能添加");
            }
        }

        iEnrollInfoService.addEnrollInfo(orderApply);
        return new Result(Code.OK, "", "");
    }

    /**
     * 获取学生是否报读过
     *
     * @param
     * @return
     */
    @GetMapping("/getStudentApplyCount")
    public Result getStudentApplyCount(@RequestParam("studentId") Long studentId, @RequestParam("subjectsId") Long subjectsId) {

        Long count = iOrderApplyService.getApplyCount(subjectsId.toString(), studentId.toString());
        //Long studentType = iOrderApplyService.getApplyCountForIsNew(subjectsId.toString(), studentId.toString());

        String phoneName = "";
        Long teacherId = null;
        EnrollCustomer enrollCustomer = iEnrollSetService.getPhoneCustomer(studentId);
        Map<String, Object> phoneTeacher = null;
        if (enrollCustomer != null) {
            teacherId = enrollCustomer.getTeacherId();
            Staff staffInfo = iStaffService.getOneById(enrollCustomer.getTeacherId());
            phoneName = staffInfo.getName();
            phoneTeacher = new HashMap<>();
            phoneTeacher.put("teacherName", phoneName);
            phoneTeacher.put("teacherId", teacherId);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("count", count);
        map.put("phoneTeacher", phoneTeacher);
        //map.put("studentType", studentType);//0为是新生，1为老生
        return new Result(Code.OK, map, "");
    }

    /**
     * 招生明细
     *
     * @param reMap
     * @return
     */
    @GetMapping("/getParticulars")
    public Result getParticulars(@RequestParam Map<String, String> reMap,
                                 @RequestParam(value = "yearClassId", defaultValue = "") String yearClassId,
                                 @RequestParam(value = "yearPart", defaultValue = "") String yearPart,
                                 @RequestParam(value = "isTrueData", defaultValue = "", required = false) String isTrueData,
                                 @RequestParam(value = "quarterNum", defaultValue = "") String quarterNum) {

        Integer pageNum = StringUtils.isEmpty((String) reMap.get("pageNum")) ? 1 : Integer.valueOf((String) reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String) reMap.get("pageSize")) ? 10 : Integer.valueOf((String) reMap.get("pageSize"));
        IPage<Map<String, Object>> iPage = new Page<>(pageNum, pageSize);

        QueryWrapper<EnrollInfo> enrollDetailQueryWrapper = new QueryWrapper<>();

        String schoolId = reMap.get("schoolId");
        String subjectsId = reMap.get("subjectsId");
        String addDate = reMap.get("startDate");
        String endDate = reMap.get("endDate");

        String studentName = reMap.get("studentName");
        String teacherName = reMap.get("teacherName");

        String checkType = reMap.get("checkType");

        List<String> groupBy = new ArrayList<>();
        groupBy.add("order_id");
        enrollDetailQueryWrapper.groupBy(groupBy);

        enrollDetailQueryWrapper.eq("deleted", 0);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(yearPart), "year_part", yearPart);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(quarterNum), "quarter_num", quarterNum);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(yearClassId), "year_class_id", yearClassId);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(isTrueData), "is_true", isTrueData);

        if (StringUtils.isNotBlank(addDate) && StringUtils.isNotBlank(endDate)) {
            enrollDetailQueryWrapper.between("add_date", addDate, endDate);
        } else if (StringUtils.isNotBlank(addDate)) {
            enrollDetailQueryWrapper.eq("add_date", addDate);
        } else if (StringUtils.isNotBlank(endDate)) {
            enrollDetailQueryWrapper.eq("add_date", endDate);
        }

        enrollDetailQueryWrapper.inSql(StringUtils.isNotBlank(studentName), "student_id", "select id from student where name like '%" + studentName + "%'");

        enrollDetailQueryWrapper.inSql(StringUtils.isNotBlank(teacherName), "teacher_id", "select id from staff where name like '%" + teacherName + "%'");

        if (StringUtils.isNotBlank(checkType)) {
            if (checkType.equals("1")) {
                enrollDetailQueryWrapper.eq("not_check_type", 0);
            } else {
                enrollDetailQueryWrapper.gt("not_check_type", 0);
            }


            /*String checkTypeSql = "";
            if ("0".equals(checkType)) {
                checkTypeSql = "( (eio <> eo AND (eio > 0 OR eo > 0)) OR (eio = eo AND eio = 0 AND eo = 0) ) ";
            }
            if ("1".equals(checkType)) {
                checkTypeSql = "eio = eo AND eio > 0 AND eo > 0 ";
            }
            enrollDetailQueryWrapper.inSql("order_id","SELECT " +
                    "order_id  " +
                    "FROM " +
                    "( " +
                        "SELECT " +
                        "ei.order_id, " +
                        "SUM(IFNULL( IF ( ei.check_type = 1, 1, 0 ), 0 )) AS eio, " +
                        "SUM(IFNULL( IF ( e.check_type = 1, 1, 0 ), 0 )) AS eo " +
                        "FROM " +
                        "enroll_info AS ei " +
                        "LEFT JOIN enroll_info AS e ON e.order_id = ei.order_id " +
                        "GROUP BY ei.id " +
                    ") AS ee  " +
                    "WHERE " +
                    checkTypeSql +
                    "GROUP BY order_id");*/
        }

        if (this.sourseLeve.equals(2)) {
            enrollDetailQueryWrapper.eq("teacher_id", this.adminId);
        }
        enrollDetailQueryWrapper.orderByDesc("id");

        iEnrollInfoService.getParticulars(iPage, enrollDetailQueryWrapper);
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

    /**
     * 招生明细
     *
     * @param reMap
     * @return
     */
    @GetMapping("/getParticularsExcel")
    public Result getParticularsExcel(@RequestParam Map<String, String> reMap) {

        Integer pageNum = StringUtils.isEmpty((String) reMap.get("pageNum")) ? 1 : Integer.valueOf((String) reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String) reMap.get("pageSize")) ? 10 : Integer.valueOf((String) reMap.get("pageSize"));
        IPage<EnrollDetail> iPage = new Page<EnrollDetail>(pageNum, pageSize);

        QueryWrapper<EnrollInfo> enrollDetailQueryWrapper = new QueryWrapper<>();

        String schoolId = reMap.get("schoolId");
        String subjectsId = reMap.get("subjectsId");
        String addDate = reMap.get("startDate");
        String studentName = reMap.get("studentName");
        String teacherName = reMap.get("teacherName");

        List<String> groupBy = new ArrayList<>();
        groupBy.add("order_id");
        enrollDetailQueryWrapper.groupBy(groupBy);

        enrollDetailQueryWrapper.eq("deleted", 0);

        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(schoolId), "school_id", schoolId);

        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);

        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(addDate), "add_date", addDate);

        enrollDetailQueryWrapper.inSql(StringUtils.isNotBlank(studentName), "student_id", "select id from student where name like '%" + studentName + "%'");

        enrollDetailQueryWrapper.inSql(StringUtils.isNotBlank(teacherName), "teacher_id", "select id from staff where name like '%" + teacherName + "%'");

        if (this.sourseLeve.equals(2)) {
            enrollDetailQueryWrapper.eq("teacher_id", this.adminId);
        }

        iEnrollInfoService.getParticulars(iPage, enrollDetailQueryWrapper);

        List<EnrollDetail> enrollInfoList = iPage.getRecords();

        String fileName = "招生明细";

        ExcelUtil.export(fileName, enrollInfoList, EnrollDetail.class, response);

        return new Result(Code.OK, null, "愿意");
    }


    /**
     * 招生提成明细
     *
     * @param reMap
     * @return
     */
    @GetMapping("/getEnrollStudent")
    public Result getEnrollStudent(@RequestParam Map reMap) {
        Integer pageNum = StringUtils.isEmpty((String) reMap.get("pageNum")) ? 1 : Integer.valueOf((String) reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String) reMap.get("pageSize")) ? 10 : Integer.valueOf((String) reMap.get("pageSize"));
        IPage<Map<String, Object>> iPage = new Page<>(pageNum, pageSize);
        QueryWrapper<EnrollInfo> enrollInfoQueryWrapper = new QueryWrapper<>();

        String schoolId = (String) reMap.get("schoolId");
        String startTime = (String) reMap.get("startTime");
        String endTIme = (String) reMap.get("endTime");

        String isGrant = (String) reMap.get("isGrant");

        enrollInfoQueryWrapper.eq(StringUtils.isNotBlank(isGrant), "a.is_grant", isGrant);
        enrollInfoQueryWrapper.eq(StringUtils.isNotBlank(schoolId), "a.school_id", schoolId);
        enrollInfoQueryWrapper.between(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTIme), "a.add_date", startTime, endTIme);
        enrollInfoQueryWrapper.groupBy("a.teacher_id");
        enrollInfoQueryWrapper.orderByAsc("a.teacher_id");
        enrollInfoQueryWrapper.eq("a.deleted", 0);
        iEnrollInfoService.getEnrollStudent(iPage, enrollInfoQueryWrapper);
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()), "");
    }

    /**
     * 老师提成明细
     */
    @GetMapping("/getEnrollForTeacherList")
    public Result getEnrollForTeacherList(@RequestParam String teacherId, @RequestParam String schoolId, @RequestParam(value = "isGrant", defaultValue = "0") String isGrant) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);
        queryWrapper.eq(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
        queryWrapper.eq("is_grant", isGrant);
        queryWrapper.eq("deleted", 0);
        List<EnrollInfo> enrollInfoList = iEnrollInfoService.getListByTeacher(queryWrapper);
        return new Result(Code.OK, enrollInfoList, "");
    }

    /**
     * 保存提成明细
     *
     * @param reMap
     * @return
     */
    @PostMapping("/settleAccounts")
    @Transactional
    public Result settleAccounts(@RequestBody Map reMap) {
        List<String> ids = (List<String>) reMap.get("ids");
        for (String id : ids) {
            EnrollInfo enrollInfo = enrollInfoMapper.selectById(id);
            enrollInfo.setIsGrant(1);
            enrollInfo.setGrantDate(LocalDate.now());
            enrollInfoMapper.updateById(enrollInfo);
        }
        return new Result(Code.OK, null, "结算成功");
    }

    /**
     * 导出招生提成表格
     *
     * @param reMap
     */
    @GetMapping("/enrollListExport")
    public void enrollListExport(@RequestParam Map reMap) {
        QueryWrapper<EnrollInfo> enrollInfoQueryWrapper = new QueryWrapper<>();
        String schoolId = (String) reMap.get("schoolId");
        String startTime = (String) reMap.get("startTime");
        String endTIme = (String) reMap.get("endTime");
        String isGrant = (String) reMap.get("isGrant");
        enrollInfoQueryWrapper.eq(StringUtils.isNotBlank(isGrant), "a.is_grant", isGrant);
        enrollInfoQueryWrapper.eq(StringUtils.isNotBlank(schoolId), "a.school_id", schoolId);
        enrollInfoQueryWrapper.between(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTIme), "a.add_date", startTime, endTIme);
        enrollInfoQueryWrapper.groupBy("a.teacher_id");
        enrollInfoQueryWrapper.orderByAsc("a.teacher_id");
        enrollInfoQueryWrapper.eq("a.deleted", 0);
        List<ExcelEnrollRecord> excelEnrollRecords = iEnrollInfoService.getExcelEnrollStudent(enrollInfoQueryWrapper);
        String fileName = "教师提成明细列表";
        ExcelUtil.export(fileName, excelEnrollRecords, ExcelEnrollRecord.class, response);
    }

    /**
     * 招生提成发放-数据汇总列表
     *
     * @param reMap
     * @return
     */
    @GetMapping("/getParticularsDataSummary")
    public Result getParticularsDataSummary(@RequestParam Map<String, String> reMap,
                                            @RequestParam(value = "yearClassId", defaultValue = "") String yearClassId,
                                            @RequestParam(value = "yearPart", defaultValue = "") String yearPart,
                                            @RequestParam(value = "isTrueData", defaultValue = "", required = false) String isTrueData,
                                            @RequestParam(value = "quarterNum", defaultValue = "") String quarterNum) {

        Integer pageNum = StringUtils.isEmpty((String) reMap.get("pageNum")) ? 1 : Integer.valueOf((String) reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String) reMap.get("pageSize")) ? 10 : Integer.valueOf((String) reMap.get("pageSize"));
        IPage<Map<String, Object>> iPage = new Page<>(pageNum, pageSize);

        QueryWrapper<EnrollInfo> enrollDetailQueryWrapper = new QueryWrapper<>();

        String schoolId = reMap.get("schoolId");
        String subjectsId = reMap.get("subjectsId");
        String addDate = reMap.get("startDate");
        String endDate = reMap.get("endDate");

        String studentName = reMap.get("studentName");
        String teacherName = reMap.get("teacherName");

        List<String> groupBy = new ArrayList<>();
        groupBy.add("teacher_id");
        enrollDetailQueryWrapper.groupBy(groupBy);

        String teacherId = reMap.get("teacherId");
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);

        enrollDetailQueryWrapper.eq("deleted", 0);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(yearPart), "year_part", yearPart);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(quarterNum), "quarter_num", quarterNum);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(yearClassId), "year_class_id", yearClassId);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(isTrueData), "is_true", isTrueData);

        if (StringUtils.isNotBlank(addDate) && StringUtils.isNotBlank(endDate)) {
            enrollDetailQueryWrapper.between("add_date", addDate, endDate);
        } else if (StringUtils.isNotBlank(addDate)) {
            enrollDetailQueryWrapper.eq("add_date", addDate);
        } else if (StringUtils.isNotBlank(endDate)) {
            enrollDetailQueryWrapper.eq("add_date", endDate);
        }

        enrollDetailQueryWrapper.inSql(StringUtils.isNotBlank(studentName), "student_id", "select id from student where name like '%" + studentName + "%'");

        enrollDetailQueryWrapper.inSql(StringUtils.isNotBlank(teacherName), "teacher_id", "select id from staff where name like '%" + teacherName + "%'");

        if (this.sourseLeve.equals(2)) {
            enrollDetailQueryWrapper.eq("teacher_id", this.adminId);
        }
        String isDescOrAsc = reMap.get("isDescOrAsc");
        String isFieldToSc = reMap.get("isFieldToSc");
        if (StringUtils.isNotBlank(isDescOrAsc) && StringUtils.isNotBlank(isFieldToSc)) {
            if ("1".equals(isDescOrAsc)) {
                enrollDetailQueryWrapper.orderByDesc(isFieldToSc);
            } else {
                enrollDetailQueryWrapper.orderByAsc(isFieldToSc);
            }
        } else {
            enrollDetailQueryWrapper.orderByDesc("id");
        }

        iEnrollInfoService.getParticularsDataSummary(iPage, enrollDetailQueryWrapper);
        return new Result(Code.OK, new PageResult<>(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

    /**
     * 招生提成发放-数据汇总列表-导出
     *
     * @param reMap
     * @return
     */
    @GetMapping("/getParticularsDataSummaryExcel")
    public Result getParticularsDataSummaryExcel(@RequestParam Map<String, String> reMap,
                                                 @RequestParam(value = "yearClassId", defaultValue = "") String yearClassId,
                                                 @RequestParam(value = "yearPart", defaultValue = "") String yearPart,
                                                 @RequestParam(value = "isTrueData", defaultValue = "", required = false) String isTrueData,
                                                 @RequestParam(value = "quarterNum", defaultValue = "") String quarterNum) {

        Integer pageNum = StringUtils.isEmpty((String) reMap.get("pageNum")) ? 1 : Integer.valueOf((String) reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String) reMap.get("pageSize")) ? 10 : Integer.valueOf((String) reMap.get("pageSize"));
        IPage<EnrollSummary> iPage = new Page<>(pageNum, pageSize);

        QueryWrapper<EnrollInfo> enrollDetailQueryWrapper = new QueryWrapper<>();

        String schoolId = reMap.get("schoolId");
        String subjectsId = reMap.get("subjectsId");
        String addDate = reMap.get("startDate");
        String endDate = reMap.get("endDate");

        String studentName = reMap.get("studentName");
        String teacherName = reMap.get("teacherName");

        List<String> groupBy = new ArrayList<>();
        groupBy.add("teacher_id");
        enrollDetailQueryWrapper.groupBy(groupBy);

        String teacherId = reMap.get("teacherId");
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);

        enrollDetailQueryWrapper.eq("deleted", 0);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(yearPart), "year_part", yearPart);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(quarterNum), "quarter_num", quarterNum);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(yearClassId), "year_class_id", yearClassId);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(isTrueData), "is_true", isTrueData);

        if (StringUtils.isNotBlank(addDate) && StringUtils.isNotBlank(endDate)) {
            enrollDetailQueryWrapper.between("add_date", addDate, endDate);
        } else if (StringUtils.isNotBlank(addDate)) {
            enrollDetailQueryWrapper.eq("add_date", addDate);
        } else if (StringUtils.isNotBlank(endDate)) {
            enrollDetailQueryWrapper.eq("add_date", endDate);
        }

        enrollDetailQueryWrapper.inSql(StringUtils.isNotBlank(studentName), "student_id", "select id from student where name like '%" + studentName + "%'");

        enrollDetailQueryWrapper.inSql(StringUtils.isNotBlank(teacherName), "teacher_id", "select id from staff where name like '%" + teacherName + "%'");

        if (this.sourseLeve.equals(2)) {
            enrollDetailQueryWrapper.eq("teacher_id", this.adminId);
        }
        enrollDetailQueryWrapper.orderByDesc("id");

        iEnrollInfoService.getParticularsDataSummary(iPage, enrollDetailQueryWrapper);

        List<EnrollSummary> enrollInfoList = iPage.getRecords();

        String fileName = "数据汇总";

        ExcelUtil.export(fileName, enrollInfoList, EnrollSummary.class, response);

        return new Result(Code.OK, null, "成功");
    }

    /**
     * 招生提成发放-提成发放列表
     *
     * @param reMap
     * @param yearClassId
     * @param yearPart
     * @param isTrueData
     * @param showType    1按教师查看，2按学生查看
     * @param quarterNum
     * @return
     */
    @GetMapping("/getParticularsGrant")
    public Result getParticularsGrant(@RequestParam Map<String, String> reMap,
                                      @RequestParam(value = "yearClassId", defaultValue = "") String yearClassId,
                                      @RequestParam(value = "yearPart", defaultValue = "") String yearPart,
                                      @RequestParam(value = "isTrueData", defaultValue = "", required = false) String isTrueData,
                                      @RequestParam(value = "startTrueDate", defaultValue = "", required = false) String startTrueDate,
                                      @RequestParam(value = "endTrueDate", defaultValue = "", required = false) String endTrueDate,
                                      @RequestParam(value = "showType", defaultValue = "1", required = false) Integer showType,
                                      @RequestParam(value = "quarterNum", defaultValue = "") String quarterNum) {

        Integer pageNum = StringUtils.isEmpty((String) reMap.get("pageNum")) ? 1 : Integer.valueOf((String) reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String) reMap.get("pageSize")) ? 10 : Integer.valueOf((String) reMap.get("pageSize"));
        IPage<Map<String, Object>> iPage = new Page<>(pageNum, pageSize);

        QueryWrapper<EnrollInfo> enrollDetailQueryWrapper = new QueryWrapper<>();

        String schoolId = reMap.get("schoolId");
        String subjectsId = reMap.get("subjectsId");
        String startDate = reMap.get("startDate");
        String endDate = reMap.get("endDate");

        String studentName = reMap.get("studentName");
        String teacherName = reMap.get("teacherName");

        //是否发放：0否，1是
        String isGrant = reMap.get("isGrant");

        String teacherId = reMap.get("teacherId");
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);

        String type = reMap.get("type");
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(type), "type", type);
        enrollDetailQueryWrapper.eq("is_true", 1);
        enrollDetailQueryWrapper.eq("check_type", 1);

        enrollDetailQueryWrapper.eq("deleted", 0);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(yearPart), "year_part", yearPart);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(quarterNum), "quarter_num", quarterNum);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(yearClassId), "year_class_id", yearClassId);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(isTrueData), "is_true", isTrueData);

        if (StringUtils.isNotBlank(startTrueDate) && StringUtils.isNotBlank(endTrueDate)) {
            enrollDetailQueryWrapper.between("true_date_time", startTrueDate+" 00:00:00", endTrueDate+" 23:59:59");
            enrollDetailQueryWrapper.between("check_date_time", startTrueDate+" 00:00:00", endTrueDate+" 23:59:59");
        } else if (StringUtils.isNotBlank(startTrueDate)) {
            enrollDetailQueryWrapper.ge("true_date_time", startTrueDate+" 00:00:00");
            enrollDetailQueryWrapper.ge("check_date_time", startTrueDate+" 00:00:00");
        } else if (StringUtils.isNotBlank(endTrueDate)) {
            enrollDetailQueryWrapper.le("true_date_time", endTrueDate+" 23:59:59");
            enrollDetailQueryWrapper.le("check_date_time", endTrueDate+" 23:59:59");
        }

        if (isGrant.equals("1")) {
            //已发放
            if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.between("grant_date_time", startDate+" 00:00:00", endDate+" 23:59:59");
            } else if (StringUtils.isNotBlank(startDate)) {
                enrollDetailQueryWrapper.ge("grant_date_time", startDate+" 00:00:00");
            } else if (StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.le("grant_date_time", endDate+" 23:59:59");
            }

            enrollDetailQueryWrapper.eq("is_grant", 1);
            enrollDetailQueryWrapper.eq("school_is_grant", 1);
        } else {
            //未发放
            if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.between("add_date", startDate, endDate);
            } else if (StringUtils.isNotBlank(startDate)) {
                enrollDetailQueryWrapper.ge("add_date", startDate);
            } else if (StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.le("add_date", endDate);
            }

            enrollDetailQueryWrapper.and(i -> i.eq("is_grant", 0).eq("school_is_grant", 0).or(j -> j.eq("is_grant", 1).eq("school_is_grant", 0)).or(j -> j.eq("is_grant", 0).eq("school_is_grant", 1)));
        }


        enrollDetailQueryWrapper.inSql(StringUtils.isNotBlank(studentName), "student_id", "select id from student where name like '%" + studentName + "%'");

        enrollDetailQueryWrapper.inSql(StringUtils.isNotBlank(teacherName), "teacher_id", "select id from staff where name like '%" + teacherName + "%'");


        if (this.sourseLeve.equals(2)) {
            enrollDetailQueryWrapper.eq("teacher_id", this.adminId);
        }

        IPage<EnrollGrantOperate> iPageSum = new Page<>(1, 10);
        iEnrollInfoService.getParticularsGrant(iPageSum, enrollDetailQueryWrapper, showType);
        Map<String, Object> other = new HashMap<>();
        other.put("commissionTotal", 0);
        if (!iPageSum.getRecords().isEmpty()) {
            other.put("commissionTotal", iPageSum.getRecords().get(0).getCommissionTotal());
        }
        List<String> groupBy = new ArrayList<>();
        if (showType.equals(1)) {
            if (isGrant.equals("1")) {
                groupBy.add("teacher_id,school_id,grant_time");
            } else {
                groupBy.add("teacher_id,school_id");
            }
        } else {
            groupBy.add("order_id");
        }

        enrollDetailQueryWrapper.groupBy(groupBy);
        String isDescOrAsc = reMap.get("isDescOrAsc");
        String isFieldToSc = reMap.get("isFieldToSc");
        if (StringUtils.isNotBlank(isDescOrAsc) && StringUtils.isNotBlank(isFieldToSc)) {
            if ("1".equals(isDescOrAsc)) {
                enrollDetailQueryWrapper.orderByDesc(isFieldToSc);
            } else {
                enrollDetailQueryWrapper.orderByAsc(isFieldToSc);
            }
        } else {
            enrollDetailQueryWrapper.orderByDesc("grant_time");
        }
        enrollDetailQueryWrapper.orderByAsc("teacher_id");
        iEnrollInfoService.getParticularsGrant(iPage, enrollDetailQueryWrapper, showType);
        return new Result(Code.OK, new PageResult<>(iPage.getRecords(), iPage.getTotal(), iPage.getPages(), other));
    }

    /**
     * 招生提成发放-提成发放列表-导出
     *
     * @param reMap
     * @return
     */
    @GetMapping("/getParticularsGrantExcel")
    public Result getParticularsGrantExcel(@RequestParam Map<String, String> reMap,
                                           @RequestParam(value = "showType", defaultValue = "1", required = false) Integer showType,
                                           @RequestParam(value = "startTrueDate", defaultValue = "", required = false) String startTrueDate,
                                           @RequestParam(value = "endTrueDate", defaultValue = "", required = false) String endTrueDate) {

        Integer pageNum = StringUtils.isEmpty((String) reMap.get("pageNum")) ? 1 : Integer.valueOf((String) reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String) reMap.get("pageSize")) ? 10 : Integer.valueOf((String) reMap.get("pageSize"));


        QueryWrapper<EnrollInfo> enrollDetailQueryWrapper = new QueryWrapper<>();

        String schoolId = reMap.get("schoolId");
        String subjectsId = reMap.get("subjectsId");
        String startDate = reMap.get("startDate");
        String endDate = reMap.get("endDate");
        String studentName = reMap.get("studentName");
        String teacherName = reMap.get("teacherName");

        //是否发放：0否，1是
        String isGrant = reMap.get("isGrant");

        List<String> groupBy = new ArrayList<>();
        if (showType.equals(1)) {
            groupBy.add("teacher_id,school_id,grant_time");
        } else {
            groupBy.add("order_id");
        }

        enrollDetailQueryWrapper.groupBy(groupBy);

        String teacherId = reMap.get("teacherId");
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(teacherId), "teacher_id", teacherId);

        String type = reMap.get("type");
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(type), "type", type);
        enrollDetailQueryWrapper.eq("is_true", 1);
        enrollDetailQueryWrapper.eq("check_type", 1);

        enrollDetailQueryWrapper.eq("deleted", 0);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(schoolId), "school_id", schoolId);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);
        enrollDetailQueryWrapper.inSql(StringUtils.isNotBlank(studentName), "student_id", "select id from student where name like '%" + studentName + "%'");
        enrollDetailQueryWrapper.inSql(StringUtils.isNotBlank(teacherName), "teacher_id", "select id from staff where name like '%" + teacherName + "%'");
        //enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(isGrant), "is_grant", isGrant);

        if (StringUtils.isNotBlank(startTrueDate) && StringUtils.isNotBlank(endTrueDate)) {
            enrollDetailQueryWrapper.between("true_date_time", startTrueDate+" 00:00:00", endTrueDate+" 23:59:59");
            enrollDetailQueryWrapper.between("check_date_time", startTrueDate+" 00:00:00", endTrueDate+" 23:59:59");
        } else if (StringUtils.isNotBlank(startTrueDate)) {
            enrollDetailQueryWrapper.ge("true_date_time", startTrueDate+" 00:00:00");
            enrollDetailQueryWrapper.ge("check_date_time", startTrueDate+" 00:00:00");
        } else if (StringUtils.isNotBlank(endTrueDate)) {
            enrollDetailQueryWrapper.le("true_date_time", endTrueDate+" 23:59:59");
            enrollDetailQueryWrapper.le("check_date_time", endTrueDate+" 23:59:59");
        }


        if (isGrant.equals("1")) {
            //已发放
            if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.between("grant_date_time", startDate+" 00:00:00", endDate+" 23:59:59");
            } else if (StringUtils.isNotBlank(startDate)) {
                enrollDetailQueryWrapper.ge("grant_date_time", startDate+" 00:00:00");
            } else if (StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.le("grant_date_time", endDate+" 23:59:59");
            }

            enrollDetailQueryWrapper.eq("is_grant", 1);
            enrollDetailQueryWrapper.eq("school_is_grant", 1);
        } else {
            //未发放
            if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.between("add_date", startDate, endDate);
            } else if (StringUtils.isNotBlank(startDate)) {
                enrollDetailQueryWrapper.ge("add_date", startDate);
            } else if (StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.le("add_date", endDate);
            }

            enrollDetailQueryWrapper.and(i -> i.eq("is_grant", 0).eq("school_is_grant", 0).or(j -> j.eq("is_grant", 1).eq("school_is_grant", 0)).or(j -> j.eq("is_grant", 0).eq("school_is_grant", 1)));
        }



        if (this.sourseLeve.equals(2)) {
            enrollDetailQueryWrapper.eq("teacher_id", this.adminId);
        }

        enrollDetailQueryWrapper.orderByDesc("grant_time");

        if (showType.equals(1)) {
            IPage<EnrollGrant> iPage = new Page<EnrollGrant>(pageNum, pageSize);
            iEnrollInfoService.getParticularsGrant(iPage, enrollDetailQueryWrapper, showType);

            List<EnrollGrant> enrollInfoList = iPage.getRecords();
            String fileName = "提成";
            if (StringUtils.isNotBlank(isGrant) && "1".equals(isGrant)) {
                ExcelUtil.export(fileName + "已发放", enrollInfoList, EnrollGrantOperate.class, response);
            } else {
                ExcelUtil.export(fileName + "未发放", enrollInfoList, EnrollGrant.class, response);
            }
        } else {
            IPage<EnrollGrantOperate> iPage = new Page<EnrollGrantOperate>(pageNum, pageSize);
            iEnrollInfoService.getParticularsGrant(iPage, enrollDetailQueryWrapper, showType);
            List<EnrollGrantOperate> enrollInfoList = iPage.getRecords();
            String fileName = "提成";

            List<EnrollGrantOperateStudent> enrollInfoStudentList = new ArrayList<>();

            for (EnrollGrantOperate enrollInfo : enrollInfoList) {
                EnrollGrantOperateStudent enrollInfoStudent = new EnrollGrantOperateStudent();
                BeanUtils.copyProperties(enrollInfo, enrollInfoStudent);
                enrollInfoStudentList.add(enrollInfoStudent);
            }

            if (StringUtils.isNotBlank(isGrant) && "1".equals(isGrant)) {
                ExcelUtil.export(fileName + "已发放", enrollInfoStudentList, EnrollGrantOperateStudent.class, response);
            } else {
                ExcelUtil.export(fileName + "未发放", enrollInfoStudentList, EnrollGrantOperateStudent.class, response);
            }
        }
        return new Result(Code.OK, null, "成功");
    }





    /**
     * 招生提成发放-提成发放详情
     *
     * @param pageNum
     * @param pageSize
     * @param showType
     * @param isGrant
     * @param type
     * @param startDate
     * @param endDate
     * @param grantTime
     * @return
     */
    @GetMapping("/getParticularsGrantDetail")
    public Result getParticularsGrantDetail(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "showType", defaultValue = "", required = false) Integer showType,
            @RequestParam(value = "isGrant", defaultValue = "", required = false) String isGrant,
            @RequestParam(value = "schoolId", defaultValue = "", required = false) String schoolId,
            @RequestParam(value = "teacherId", defaultValue = "", required = false) String teacherId,
            @RequestParam(value = "type", defaultValue = "", required = false) String type,
            @RequestParam(value = "startDate", defaultValue = "", required = false) String startDate,
            @RequestParam(value = "endDate", defaultValue = "", required = false) String endDate,
            @RequestParam(value = "startTrueDate", defaultValue = "", required = false) String startTrueDate,
            @RequestParam(value = "endTrueDate", defaultValue = "", required = false) String endTrueDate,
            @RequestParam(value = "grantTime", defaultValue = "", required = false) Long grantTime) {

        //String startTrueDate = "";
        //String endTrueDate = "";
        if (StringUtils.isNotBlank(startTrueDate)){
            startTrueDate =  startTrueDate+" 00:00:00";
        }
        if (StringUtils.isNotBlank(endTrueDate)){
            endTrueDate =  endTrueDate+ " 23:59:59";
        }

        IPage<Map<String, Object>> iPage = new Page<>(pageNum, pageSize);

        QueryWrapper<EnrollInfo> enrollDetailQueryWrapper = new QueryWrapper<>();

        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(type), "type", type);
        enrollDetailQueryWrapper.eq("is_true", 1);
        enrollDetailQueryWrapper.eq("check_type", 1);
        enrollDetailQueryWrapper.eq("deleted", 0);
        enrollDetailQueryWrapper.eq("school_id", schoolId);
        enrollDetailQueryWrapper.eq("teacher_id", teacherId);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(isGrant), "is_grant", isGrant);

        /*if (isGrant.equals("1")) {
            if (grantTime != null) {
                enrollDetailQueryWrapper.eq("grant_time", grantTime);
            }
        } else {
            if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.between("add_date", startDate, endDate);
            } else if (StringUtils.isNotBlank(startDate)) {
                enrollDetailQueryWrapper.eq("add_date", startDate);
            } else if (StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.eq("add_date", endDate);
            }
        }*/

        if (StringUtils.isNotBlank(startTrueDate) && StringUtils.isNotBlank(endTrueDate)) {
            enrollDetailQueryWrapper.between("true_date_time", startTrueDate+" 00:00:00", endTrueDate+" 23:59:59");
            enrollDetailQueryWrapper.between("check_date_time", startTrueDate+" 00:00:00", endTrueDate+" 23:59:59");
        } else if (StringUtils.isNotBlank(startTrueDate)) {
            enrollDetailQueryWrapper.ge("true_date_time", startTrueDate+" 00:00:00");
            enrollDetailQueryWrapper.ge("check_date_time", startTrueDate+" 00:00:00");
        } else if (StringUtils.isNotBlank(endTrueDate)) {
            enrollDetailQueryWrapper.le("true_date_time", endTrueDate+" 23:59:59");
            enrollDetailQueryWrapper.le("check_date_time", endTrueDate+" 23:59:59");
        }

        if (isGrant.equals("1")) {
            //已发放
            /*if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.between("grant_date_time", startDate+" 00:00:00", endDate+" 23:59:59");
            } else if (StringUtils.isNotBlank(startDate)) {
                enrollDetailQueryWrapper.ge("grant_date_time", startDate+" 00:00:00");
            } else if (StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.le("grant_date_time", endDate+" 23:59:59");
            }

            enrollDetailQueryWrapper.eq("is_grant", 1);
            enrollDetailQueryWrapper.eq("school_is_grant", 1);*/
            if (grantTime != null) {
                enrollDetailQueryWrapper.eq("grant_time", grantTime);
            }
        } else {
            //未发放
            if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.between("add_date", startDate, endDate);
            } else if (StringUtils.isNotBlank(startDate)) {
                enrollDetailQueryWrapper.ge("add_date", startDate);
            } else if (StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.le("add_date", endDate);
            }

            enrollDetailQueryWrapper.and(i -> i.eq("is_grant", 0).eq("school_is_grant", 0).or(j -> j.eq("is_grant", 1).eq("school_is_grant", 0)).or(j -> j.eq("is_grant", 0).eq("school_is_grant", 1)));
        }



        IPage<EnrollGrantOperate> iPageSum = new Page<>(pageNum, pageSize);
        iEnrollInfoService.getParticularsGrant(iPageSum, enrollDetailQueryWrapper, showType);
        Map<String, Object> other = new HashMap<>();
        other.put("commissionTotal", 0);
        if (!iPageSum.getRecords().isEmpty()) {
            other.put("commissionTotal", iPageSum.getRecords().get(0).getCommissionTotal());
        }

        List<String> groupBy = new ArrayList<>();
        groupBy.add("id");
        enrollDetailQueryWrapper.groupBy(groupBy);
        enrollDetailQueryWrapper.orderByDesc("order_id");
        iEnrollInfoService.getParticularsGrantDetail(iPage, enrollDetailQueryWrapper);
        return new Result(Code.OK, new PageResult<>(iPage.getRecords(), iPage.getTotal(), iPage.getPages(), other));
    }



    /**
     * 招生提成发放-提成发放保存
     *
     * @param
     * @return
     */
    @RepeatSubmit(expireSeconds = 5)
    @PostMapping("/particularsGrantSave")
    public Result particularsGrantSave(@RequestBody EnrollInfo enrollInfo) {

        Long schoolId = enrollInfo.getSchoolId();
        Long teacherId = enrollInfo.getTeacherId();
        BigDecimal pushMoney = enrollInfo.getPushMoney();

        String startDate = "";
        String endDate = "";
        if (enrollInfo.getMinAddDate() != null) {
            startDate = enrollInfo.getMinAddDate().toString();
        }
        if (enrollInfo.getMaxAddDate() != null) {
            endDate = enrollInfo.getMaxAddDate().toString();
        }

        if (schoolId == null) {
            return new Result(Code.ERROR, null, "schoolId不能为空！");
        }

        if (teacherId == null) {
            return new Result(Code.ERROR, null, "teacherId不能为空！");
        }

        if (pushMoney == null) {
            return new Result(Code.ERROR, null, "发放金额不能为空！");
        }

        pushMoney = pushMoney.setScale(1, BigDecimal.ROUND_HALF_UP);

        String startTrueDate = "";
        String endTrueDate = "";
        if (enrollInfo.getStartTrueDate()!=null){
            startTrueDate =  enrollInfo.getStartTrueDate()+" 00:00:00";
        }
        if (enrollInfo.getEndTrueDate()!=null){
            endTrueDate =  enrollInfo.getEndTrueDate()+ " 23:59:59";
        }

        long timestamp = System.currentTimeMillis();//当前时间戳
        LocalDate now = LocalDate.now();
        Long adminId = this.adminId;
        Staff staffInfo = iStaffService.getOneById(adminId);
        BigDecimal pushMoneyTotal = iEnrollInfoService.getParticularsTotal(schoolId, teacherId, startDate, endDate,startTrueDate,endTrueDate);
        pushMoneyTotal = pushMoneyTotal.setScale(1, BigDecimal.ROUND_HALF_UP);
        if (!pushMoneyTotal.equals(pushMoney)) {
            return new Result(Code.ERROR, null, "发放金额异常，请返回刷新重新发放！");
        }

        Long grantNo = idWorker.nextId();

        iEnrollInfoService.particulartsUpdate(schoolId, teacherId, startDate, endDate, now.toString(), timestamp, staffInfo.getId(), staffInfo.getName(),startTrueDate,endTrueDate,grantNo);

        /*
                enrollInfoUpdateWrapper.set("is_grant",1);
                enrollInfoUpdateWrapper.set("grant_date",now);
                enrollInfoUpdateWrapper.set("grant_time",timestamp);
                enrollInfoUpdateWrapper.set("operate_teacher_id",adminId);
                enrollInfoUpdateWrapper.set("operate_teacher_name",staffInfo.getName());
         */


        return new Result(Code.OK, null, "提成发放成功！");
    }

    /**
     * 批量招生提成发放-提成发放保存
     */
    @RepeatSubmit(expireSeconds = 5)
    @PostMapping("/particularsGrantBatchSave")
    public Result particularsGrantBatchSave(@RequestBody EnrollInfo enrollInfo) {
        if (enrollInfo.getChildrenList() == null || enrollInfo.getChildrenList().size() == 0) {
            return new Result(Code.ERROR, null, "请选择需要发放的数据!");
        }

        LocalDate now = LocalDate.now();
        long timestamp = System.currentTimeMillis();//当前时间戳
        for (EnrollInfo info : enrollInfo.getChildrenList()) {
            Long schoolId = info.getSchoolId();
            Long teacherId = info.getTeacherId();
            BigDecimal pushMoney = info.getPushMoney();

            String startDate = "";
            String endDate = "";
            if (info.getMinAddDate() != null) {
                startDate = info.getMinAddDate().toString();
            }
            if (info.getMaxAddDate() != null) {
                endDate = info.getMaxAddDate().toString();
            }

            if (schoolId == null) {
                return new Result(Code.ERROR, null, "schoolId不能为空！");
            }

            if (teacherId == null) {
                return new Result(Code.ERROR, null, "teacherId不能为空！");
            }

            if (pushMoney == null) {
                return new Result(Code.ERROR, null, "发放金额不能为空！");
            }

            pushMoney = pushMoney.setScale(1, BigDecimal.ROUND_HALF_UP);

            String startTrueDate = "";
            String endTrueDate = "";
            if (info.getStartTrueDate()!=null){
                startTrueDate =  info.getStartTrueDate()+" 00:00:00";
            }
            if (info.getEndTrueDate()!=null){
                endTrueDate =  info.getEndTrueDate()+ " 23:59:59";
            }
            //Long adminId = this.adminId;
            //Staff staffInfo = iStaffService.getOneById(adminId);
            Staff teacherInfo = iStaffService.getOneById(teacherId);
            BigDecimal pushMoneyTotal = iEnrollInfoService.getParticularsTotal(schoolId, teacherId, startDate, endDate,startTrueDate,endTrueDate);
            pushMoneyTotal = pushMoneyTotal.setScale(1, BigDecimal.ROUND_HALF_UP);
            if (!pushMoneyTotal.equals(pushMoney)) {
                return new Result(Code.ERROR, null, teacherInfo.getName()+"发放金额不一致"+pushMoney+"-"+pushMoneyTotal+"，请与智能部联系！");
            }
        }

        for (EnrollInfo info : enrollInfo.getChildrenList()) {
            Long schoolId = info.getSchoolId();
            Long teacherId = info.getTeacherId();
            BigDecimal pushMoney = info.getPushMoney();
            String startDate = "";
            String endDate = "";
            if (info.getMinAddDate() != null) {
                startDate = info.getMinAddDate().toString();
            }
            if (info.getMaxAddDate() != null) {
                endDate = info.getMaxAddDate().toString();
            }

            String startTrueDate = "";
            String endTrueDate = "";
            if (info.getStartTrueDate()!=null){
                startTrueDate =  info.getStartTrueDate()+" 00:00:00";
            }
            if (info.getEndTrueDate()!=null){
                endTrueDate =  info.getEndTrueDate()+ " 23:59:59";
            }

            Long grantNo = idWorker.nextId();
            iEnrollInfoService.particulartsUpdate(schoolId, teacherId, startDate, endDate, now.toString(), timestamp, staffInfo.getId(), staffInfo.getName(),startTrueDate,endTrueDate,grantNo);
        }



        return new Result(Code.OK, null, "提成发放成功！");
    }

    /**
     * 招生提成发放-提成发放详情-导出
     *
     * @param pageNum
     * @param pageSize
     * @param showType
     * @param isGrant
     * @param type
     * @param startDate
     * @param endDate
     * @param grantTime
     * @return
     */
    @GetMapping("/getParticularsGrantDetailExcel")
    public Result getParticularsGrantDetailExcel(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                 @RequestParam(value = "showType", defaultValue = "", required = false) Integer showType,
                                                 @RequestParam(value = "isGrant", defaultValue = "", required = false) String isGrant,
                                                 @RequestParam(value = "schoolId", defaultValue = "", required = false) String schoolId,
                                                 @RequestParam(value = "teacherId", defaultValue = "", required = false) String teacherId,
                                                 @RequestParam(value = "type", defaultValue = "", required = false) String type,
                                                 @RequestParam(value = "startDate", defaultValue = "", required = false) String startDate,
                                                 @RequestParam(value = "endDate", defaultValue = "", required = false) String endDate,
                                                 @RequestParam(value = "grantTime", defaultValue = "", required = false) Long grantTime,
                                                 @RequestParam(value = "startTrueDate", defaultValue = "", required = false) String startTrueDate,
                                                 @RequestParam(value = "endTrueDate", defaultValue = "", required = false) String endTrueDate) {

        IPage<EnrollGrantDetail> iPage = new Page<>(pageNum, pageSize);

        QueryWrapper<EnrollInfo> enrollDetailQueryWrapper = new QueryWrapper<>();

        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(type), "type", type);
        enrollDetailQueryWrapper.eq("is_true", 1);
        enrollDetailQueryWrapper.eq("check_type", 1);
        enrollDetailQueryWrapper.eq("deleted", 0);
        enrollDetailQueryWrapper.eq("school_id", schoolId);
        enrollDetailQueryWrapper.eq("teacher_id", teacherId);
        enrollDetailQueryWrapper.eq(StringUtils.isNotBlank(isGrant), "is_grant", isGrant);

        if (StringUtils.isNotBlank(startTrueDate) && StringUtils.isNotBlank(endTrueDate)) {
            enrollDetailQueryWrapper.between("true_date_time", startTrueDate+" 00:00:00", endTrueDate+" 23:59:59");
            enrollDetailQueryWrapper.between("check_date_time", startTrueDate+" 00:00:00", endTrueDate+" 23:59:59");
        } else if (StringUtils.isNotBlank(startTrueDate)) {
            enrollDetailQueryWrapper.ge("true_date_time", startTrueDate+" 00:00:00");
            enrollDetailQueryWrapper.ge("check_date_time", startTrueDate+" 00:00:00");
        } else if (StringUtils.isNotBlank(endTrueDate)) {
            enrollDetailQueryWrapper.le("true_date_time", endTrueDate+" 23:59:59");
            enrollDetailQueryWrapper.le("check_date_time", endTrueDate+" 23:59:59");
        }

        if (isGrant.equals("1")) {

            if (grantTime != null) {
                enrollDetailQueryWrapper.eq("grant_time", grantTime);
            }
            //已发放
            /*if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.between("grant_date_time", startDate+" 00:00:00", endDate+" 23:59:59");
            } else if (StringUtils.isNotBlank(startDate)) {
                enrollDetailQueryWrapper.ge("grant_date_time", startDate+" 00:00:00");
            } else if (StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.le("grant_date_time", endDate+" 23:59:59");
            }

            enrollDetailQueryWrapper.eq("is_grant", 1);
            enrollDetailQueryWrapper.eq("school_is_grant", 1);*/
        } else {
            //未发放
            if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.between("add_date", startDate, endDate);
            } else if (StringUtils.isNotBlank(startDate)) {
                enrollDetailQueryWrapper.ge("add_date", startDate);
            } else if (StringUtils.isNotBlank(endDate)) {
                enrollDetailQueryWrapper.le("add_date", endDate);
            }

            enrollDetailQueryWrapper.and(i -> i.eq("is_grant", 0).eq("school_is_grant", 0).or(j -> j.eq("is_grant", 1).eq("school_is_grant", 0)).or(j -> j.eq("is_grant", 0).eq("school_is_grant", 1)));
        }

        List<String> groupBy = new ArrayList<>();
        groupBy.add("id");
        enrollDetailQueryWrapper.groupBy(groupBy);
        enrollDetailQueryWrapper.orderByDesc("order_id");

        iEnrollInfoService.getParticularsGrantDetail(iPage, enrollDetailQueryWrapper);

        List<EnrollGrantDetail> enrollInfoList = iPage.getRecords();

        String fileName = "提成发放详情";

        if (StringUtils.isNotBlank(isGrant) && "1".equals(isGrant)) {
            fileName = "已发放提成详情";
        }
        if (StringUtils.isNotBlank(isGrant) && "0".equals(isGrant)) {
            fileName = "未发放提成详情";
        }
        ExcelUtil.export(fileName, enrollInfoList, EnrollGrantDetail.class, response);

        return new Result(Code.OK, null, "愿意");
    }

}
